package com.uphone.kingmall.activity.personal.set;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.adapter.BankListAdapter;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.UserBanksBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.view.dialog.OnDialogViewClickListener;
import com.uphone.kingmall.view.dialog.RadishDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 王雨
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2018/07/31
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class MyBankListActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rv_bank_list)
    RecyclerView rvBankList;
    @BindView(R.id.rl_bind)
    RelativeLayout rlBind;
    List list = new ArrayList();
    private BankListAdapter adapter;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_my_bank_list;
    }

    @Override
    public void initView() {
        setStatusBar();
    }

    @Override
    public void initData() {
        rvBankList.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BankListAdapter(R.layout.kingmall_item_bank, list);
        rvBankList.setAdapter(adapter);
        adapter.setOnBankItemClickListener(new BankListAdapter.BankItemClickListener() {
            @Override
            public void delBankCard(View v, int psotion, UserBanksBean.BankcardsBean item) {
                delDialogShow(item);
            }
        });
    }

    private void delDialogShow(final UserBanksBean.BankcardsBean item) {
        new RadishDialog.Builder(this).setView(R.layout.kingmall_dialog_alter)
                .setText(R.id.dialog_message, "确定解除绑定？")
                .setClick(R.id.dialog_submit, new OnDialogViewClickListener() {
                    @Override
                    public void onClick(final RadishDialog dialog, View view) {
                        HttpParams params = new HttpParams();
                        params.put("bankcardId", item.getId());
                        OkGoUtils.progressRequest(MyUrl.delUserBankcard, MyBankListActivity.this, params, new onNormalRequestListener() {
                            @Override
                            public void onSuccess(String response, int code) {
                                if (code == 0) {
                                    ToastUtils.showShortToast(MyBankListActivity.this, "解绑成功");
                                    loadData();
                                }
                            }

                            @Override
                            public void onError(Response<String> response) {

                            }
                        });
                        dialog.dismiss();
                    }
                })
                .setClick(R.id.dialog_cancel, new OnDialogViewClickListener() {
                    @Override
                    public void onClick(RadishDialog dialog, View view) {
                        dialog.dismiss();
                    }
                }).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    private void loadData() {
        OkGoUtils.normalRequest(MyUrl.getUserBankCards, null, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    UserBanksBean bean = GsonUtils.getGson().fromJson(body, UserBanksBean.class);
                    if (bean != null && bean.getBankcards() != null) {
                        adapter.setNewData(bean.getBankcards());
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    @OnClick({R.id.iv_back, R.id.rl_bind})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_bind:
                openActivity(BankInfo1Activity.class);
                break;
        }
    }
}
